import SwiftUI
import ActivityIndicatorView
import PopupView

struct ContentView: View {
    
    @StateObject var fsDefaults = NcdDefaults()
    @State var fsRouter = Router()
    
    @State private var indicator = IndicatorModel()
    @State private var popupModel = PopupModel()
    private var networkMonitor = NetworkMonitor()
    private var locationService = LocationService()
    @State var currentSelected: Tab = .food
    
    var body: some View {
        NavigationStack(path: $fsRouter.path) {
            VStack(spacing: 0) {
                switch currentSelected {
                case .food:
                    FoodHomeView(showFoodType: true)
                case .store:
                    FoodHomeView(showFoodType: false)
                }
                Spacer(minLength: 0)
                BaseTabbarView(currentSelected: $currentSelected)
            }
            .ignoresSafeArea(.keyboard)
            .navigationDestination(for: AppRoute.self) { route in
                switch route {
                case .foodDetailView(let foodId):
                    FoodDetailView(foodId: foodId)
                case .storeDetailView(let storeId):
                    StoreDetailView(storeId: storeId)
                case .searchFoodStoreView(let search):
                    SearchFoodStoreView(searchContent: search)
                }
            }
        }
        .overlay(alignment: .center) {
            if indicator.showLoadingIndicator {
                switch indicator.tipContent {
                case .progressLoading:
                    ProgressNcdView(auto: true)
                default:
                    VStack(spacing: 0) {
                        ActivityIndicatorView(isVisible: $indicator.showLoadingIndicator, type: .opacityDots())
                            .frame(width: 60, height: 60)
                            .foregroundStyle(.fsFFFFFF)
                        Text(indicator.tip)
                            .font(.alibabaPuHuiTi(size: 16.0, weight: .semiBold))
                            .foregroundStyle(.fsFFFFFF)
                    }
                    .padding(.horizontal, 32.0)
                    .padding(.vertical, 16.0)
                    .background(.fs575958.opacity(0.75))
                    .clipShape(RoundedRectangle(cornerRadius: 12.0))
                }
                
            }
        }
        .popup(isPresented: $popupModel.showPopupView) {
            switch popupModel.contentTyep {
            case .message:
                messageContent()
            case .progress:
                ProgressNcdView(auto: true)
            }
        } customize: {
            switch popupModel.contentTyep {
            case .message:
                $0.autohideIn(2.4)
                  .type(.floater())
                  .closeOnTap(false)
                  .position(.top)
            case .progress:
                $0.autohideIn(nil)
                  .type(.floater())
                  .closeOnTap(false)
                  .position(.top)
            }
            
        }
        .onChange(of: locationService.error, { _, newValue in
            if let newValue {
                popupModel.show(content: newValue.message)
            }
        })
        .environmentObject(fsDefaults)
        .environment(fsRouter)
        .environment(indicator)
        .environment(popupModel)
        .environment(networkMonitor)
        .environment(locationService)
        .onAppear {
            Log.info("根视图出现")
            locationService.requestLocation()
        }
    }
    
    private func messageContent() -> some View {
        HStack {
            Text(popupModel.content)
                .font(.alibabaPuHuiTi(size: 16.0, weight: .medium))
                .foregroundStyle(Color.fsFFFFFF)
        }
        .padding(.vertical, 16.0)
        .padding(.horizontal, 32.0)
        .background(.fs020000.opacity(0.5))
        .clipShape(RoundedRectangle(cornerRadius: 12.0))
    }
}

#Preview {
    ContentView()
}
